package com.java.dao.Impl;

import com.java.dao.AdminDao;
import com.java.entity.Admins;
import com.java.entity.Orders;
import com.java.entity.Users;
import com.java.util.C3P0Util;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AdminDaoImpl implements AdminDao {
    private QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());

    @Override
    public Admins selectAdminByUserNameAndPassword(String userName, String password) throws SQLException{
    String sql = "select * from f_admin where username=? and password=? ";
    Admins admins= qr.query(sql, new ResultSetHandler<Admins>() {
        Admins admin = null;
        @Override
        public Admins handle(ResultSet resultSet) throws SQLException {
            if(resultSet.next()){
                admin = new Admins();
                admin.setAdminId(resultSet.getString("adminid"));
                admin.setUserName(resultSet.getString("userName"));
                admin.setPassword(resultSet.getString("password"));
            }
            return admin;
        }
    },userName,password);
    return admins;
    }

    @Override
    public List<Orders> selectAllOrders() throws SQLException {
        String sql = "select * from f_order";
        List<Orders> ordersList=qr.query(sql, new ResultSetHandler<List<Orders>>() {
            @Override
            public List<Orders> handle(ResultSet resultSet) throws SQLException {
                List<Orders> orders = new ArrayList<>();
                while (resultSet.next()){
                    Orders order = new Orders();
                    order.setBookId(resultSet.getString("bookId"));     //                    private String bookId;//订单号
                    order.setName(resultSet.getString("name"));     //                    private String name;//姓名
                    order.setSex(resultSet.getString("sex"));     //                    private String sex;//性别
                    order.setAge(resultSet.getInt("age"));     //                    private int age;//年龄
                    order.setId(resultSet.getString("Id"));     //                    private String id;//身份证号码
                    order.setPhone(resultSet.getString("phone"));     //                    private String phone;//手机号码
                    order.setAddress(resultSet.getString("address"));     //                    private String address;//家庭住址
                    order.setOrigin(resultSet.getString("Origin"));     //                    private String origin;//出发地
                    order.setDestination(resultSet.getString("destination"));    //                    private String destination;//目的地
                    order.setLeaveTime(resultSet.getString("leaveTime"));     //                    private String leaveTime;//出发时间
                    order.setArriveTime(resultSet.getString("arriveTime"));     //                    private String arriveTime;//到达时间
                    order.setFlightNo(resultSet.getString("flightNo"));     //                    private String flightNo;//航班号
                    order.setAirlineNo(resultSet.getString("AirlineNo"));     //                    private String airlineNo;//航线号
                    order.setTicketPrice(resultSet.getInt("ticketPrice"));     //                    private int ticketPrice;//票价
                    order.setPaydastatus(resultSet.getInt("paydastatus"));     //                    private int paydastatus;//支付状态 0表示未支付 1表示已支付
                    order.setInsurance(resultSet.getInt("insurance"));     //                    private int insurance;//是否购买保险 0表示未购买 1表示购买
                    order.setLeave(resultSet.getString("leave"));     //                    private String leave;//离开日期
                    order.setArrive(resultSet.getString("arrive"));     //                    private String arrive;//到达日期

                    orders.add(order);
                }
                return orders;
            }
        });
        return ordersList;
    }

    @Override
    public Orders selectOrdersByBookId(String bookId) throws SQLException {
        Orders orders = new Orders();
        String sql = "select * from f_order where bookId=?";
        orders = qr.query(sql, new ResultSetHandler<Orders>() {
            @Override
            public Orders handle(ResultSet resultSet) throws SQLException {
                Orders order = new Orders();
                if (resultSet.next()){
                    order.setAddress(resultSet.getString("address"));
                    order.setAge(resultSet.getInt("age"));
                    order.setAirlineNo(resultSet.getString("airlineNo"));
                    order.setArriveTime(resultSet.getString("arriveTime"));
                    order.setBookId(resultSet.getString("bookId"));
                    order.setDestination(resultSet.getString("destination"));
                    order.setFlightNo(resultSet.getString("flightNo"));
                    order.setId(resultSet.getString("id"));
                    order.setInsurance(resultSet.getInt("insurance"));
                    order.setLeaveTime(resultSet.getString("leaveTime"));
                    order.setName(resultSet.getString("name"));
                    order.setOrigin(resultSet.getString("origin"));
                    order.setPaydastatus(resultSet.getInt("paydastatus"));
                    order.setPhone(resultSet.getString("phone"));
                    order.setSex(resultSet.getString("sex"));
                    order.setTicketPrice(resultSet.getInt("ticketPrice"));
                }
                return order;
            }
        },bookId);
        return orders;
    }

    @Override
    public boolean updateOrderById(Orders orders) throws SQLException {
        String sql = "update f_order set name=? ,sex=?, age=?,id=?,phone=?,address=?,origin=?,\n" +
                "destination=?,ticketPrice=? where bookid=?";
        int i = qr.update(sql, orders.getName(), orders.getSex(), orders.getAge(), orders.getId(), orders.getPhone(),
                orders.getAddress(), orders.getOrigin(), orders.getDestination(), orders.getTicketPrice(), orders.getBookId());
        boolean flag = false;
        if (i != 0){
            flag = true;
        }
        return flag;
    }

    @Override
    public List<Users> selectAllUsers() throws SQLException {
        String sql = "select * from f_users";
        List<Users> usersList=qr.query(sql, new ResultSetHandler<List<Users>>() {
            @Override
            public List<Users> handle(ResultSet resultSet) throws SQLException {
                List<Users> users = new ArrayList<>();
                while (resultSet.next()){
                    Users user = new Users();
                    user.setAddress(resultSet.getString("address"));
                    user.setAge(resultSet.getInt("age"));
                    user.setId(resultSet.getString("id"));
                    user.setName(resultSet.getString("name"));
                    user.setPassword(resultSet.getString("password"));
                    user.setPhone(resultSet.getString("phone"));
                    user.setSex(resultSet.getString("sex"));
                    user.setUserid(resultSet.getString("userId"));
                    users.add(user);
                }
                return users;
            }
        });
        return usersList;
    }

    @Override
    public Users selectUserById(String id) throws SQLException {
        String sql = "select * from f_users where id=?";
        Users user = qr.query(sql, new ResultSetHandler<Users>() {
            @Override
            public Users handle(ResultSet resultSet) throws SQLException {
                Users users = new Users();
                if (resultSet.next()){
                    users.setUserid(resultSet.getString("userid"));
                    users.setSex(resultSet.getString("sex"));
                    users.setPhone(resultSet.getString("phone"));
                    users.setPassword(resultSet.getString("password"));
                    users.setName(resultSet.getString("name"));
                    users.setAge(resultSet.getInt("age"));
                    users.setAddress(resultSet.getString("address"));
                    users.setId(resultSet.getString("id"));
                }
                return users;
            }
        },id);
        return user;
    }

    @Override
    public boolean updateUserById(Users users) throws SQLException {
        String sql = "update f_users set userId=?,password=?,name=?,sex=?,age=?,phone=?,address=? where id=?";
        int i = qr.update(sql,users.getUserid(),users.getPassword(),users.getName(),users.getSex(),users.getAge(),users.getPhone(),users.getAddress(),users.getId());
        boolean flag = false;
        if (i != 0){
             flag = true;
        }
        return flag;
    }
    @Override
    public List<Admins> selectAllAdmins() throws SQLException {
        String sql = "select * from f_admin";
        System.out.println(sql);
        List<Admins> adminsList = qr.query(sql, new ResultSetHandler<List<Admins>>() {
            @Override
            public List<Admins> handle(ResultSet resultSet) throws SQLException {
                List<Admins> list = new ArrayList<>();
                while(resultSet.next()){
                    Admins admins = new Admins();
                    admins.setAdminId(resultSet.getString("ADMINID"));
                    admins.setUserName(resultSet.getString("USERNAME"));
                    admins.setPassword(resultSet.getString("PASSWORD"));
                    list.add(admins);
                }
                return list;
            }
        });
        return adminsList;
    }

    @Override
    public Admins checkAdminById(String adminId) throws SQLException {
        String sql = "select * from f_admin where adminId=?";
        Admins admins=qr.query(sql, new ResultSetHandler<Admins>() {
            @Override
            public Admins handle(ResultSet resultSet) throws SQLException {
                Admins admin = new Admins();
                while (resultSet.next()){
                    admin.setUserName(resultSet.getString("userName"));
                    admin.setPassword(resultSet.getString("password"));
                    admin.setAdminId(resultSet.getString("adminId"));
                }
                return admin;
            }
        },adminId);
        return admins;
    }

    @Override
    public boolean updateAdminById(Admins admin) throws SQLException {
        String sql  = "update f_admin set userName=?,password=? where adminId = ?";
        boolean flag =true;
        int i = qr.update(sql, admin.getUserName(), admin.getPassword(), admin.getAdminId());
        if (i != 0){
            return flag;
        }
        return flag = false;
    }

    @Override
    public int addAdmin(String id,String username, String password) throws SQLException {
        String sql="insert into f_admin values(?,?,?)";
        int i=qr.update(sql,id,username,password);
        return i;
    }
}
